<!DOCTYPE html>
<html lang="zh-CN">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>计算两个日期之间的工作日</title>
    <style>
        body {
            font-family: 'Arial', sans-serif;
            background-color: #f4f7fa;
            color: #333;
            margin: 0;
            padding: 0;
            display: flex;
            justify-content: center;
            align-items: center;
            height: 100vh;
        }

        .container {
            background-color: #ffffff;
            box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
            padding: 20px;
            border-radius: 8px;
            width: 400px;
            text-align: center;
        }

        h1 {
            color: #4CAF50;
            margin-bottom: 20px;
        }

        label {
            font-size: 16px;
            display: block;
            margin-bottom: 8px;
            text-align: left;
        }

        input {
            width: 100%;
            padding: 10px;
            margin: 10px 0;
            border: 1px solid #ddd;
            border-radius: 4px;
            font-size: 16px;
            box-sizing: border-box;
        }

        button {
            background-color: #4CAF50;
            color: white;
            border: none;
            padding: 10px 20px;
            border-radius: 4px;
            font-size: 16px;
            cursor: pointer;
            transition: background-color 0.3s;
        }

        button:hover {
            background-color: #45a049;
        }

        .result {
            margin-top: 20px;
            font-size: 18px;
            font-weight: bold;
            color: #333;
        }

        .error {
            color: red;
        }
    </style>
</head>

<body>

    <div class="container">
        <h1>计算两个日期之间的工作日</h1>

        <label for="startDate">选择开始日期：</label>
        <input type="date" id="startDate" />

        <label for="endDate">选择结束日期：</label>
        <input type="date" id="endDate" />

        <button onclick="calculateWeekDays()">计算工作日数</button>

        <div class="result" id="result"></div>
    </div>

    <script>
        const countWeekDaysBetween = (startDate, endDate) =>
            Array.from({ length: (endDate - startDate) / (1000 * 3600 * 24) }).reduce(
                (count) => {
                    if (startDate.getDay() % 6 !== 0) {
                        count++;
                    }
                    startDate = new Date(startDate.setDate(startDate.getDate() + 1));
                    return count;
                },
                0
            );

        function calculateWeekDays() {
            const startDateInput = document.getElementById('startDate').value;
            const endDateInput = document.getElementById('endDate').value;
            const resultDiv = document.getElementById('result');

            // 清空错误信息
            resultDiv.classList.remove('error');

            if (!startDateInput || !endDateInput) {
                resultDiv.textContent = '请输入完整的日期!';
                resultDiv.classList.add('error');
                return;
            }

            const startDate = new Date(startDateInput);
            const endDate = new Date(endDateInput);

            if (endDate < startDate) {
                resultDiv.textContent = '结束日期不能早于开始日期!';
                resultDiv.classList.add('error');
                return;
            }

            const weekDaysCount = countWeekDaysBetween(startDate, endDate);
            resultDiv.textContent = `两个日期之间的工作日数为：${weekDaysCount}天`;
        }
    </script>

</body>

</html>